;
(function() {
	var skill = {
		//代码片段，使用ID获取指定订单
		_getAch: function(ach_id, without_getCash) {
			var achs = this.achs || [];
			if (achs.indexOf(ach_id) === -1) {
				throwCusError("订单不属于指定用户")
			}
			var ach = AchCon.get(ach_id);
			//运行BUFF获取sou_cash和cash（原价与应付款额）
			if (!ach) {
				throwCusError("找不到指定订单");
			}
			without_getCash || ach.getCash();
			return ach;
		},
		//获取订单信息
		getAchs: function(num, page, filter) {
			var self = this;
			var achs = (self.achs || []).slice();
			if (filter) {
				var result = [];
				achs.forEach(function(ach_id) {
					var ach = AchCon.get(ach_id)
					if (!ach) {
						self._removeAch(ach_id);
						return
					}
					if (filter(ach)) {
						result.push(ach);
					}
				});
				result = data_cut.page(result, num, page)
			} else {
				achs = data_cut.page(achs, num, page);
				result = achs.map(function(ach_id) {
					var ach = AchCon.get(ach_id);
					if (!ach) {
						self._removeAch(ach_id);
						return
					}
					return ach;
				});
			}
			return result;
		},
		getAchsWithType: function(type, num, page) {
			if (type === "未支付") {
				var achs = this.getAchs(num, page, function(ach) {
					//前者等待用户付款后确认收款，这个期间默认为未付款
					return ach.info.state == AchCon.STATE.确认收获并付款 || ach.info.state == AchCon.STATE.未付款
				});
			} else if (type && typeof type === "string") {
				var types = type.split("|");
				var achs = this.getAchs(num, page, function(ach) {
					return types.some(function(type) {
						return ach.info.state == AchCon.STATE[type]
					});
				});
			} else {
				var achs = this.getAchs(num, page);
			}
			return achs;
		},
		getAchsWithType_PageInfo: function(type, num, page) {
			var achs = this.getAchsWithType(type);
			var total_num = achs.length;
			//分页
			achs = data_cut.page(achs, num, page);
			//加上用户信息
			var res = [];
			achs.forEach(function(ach) {
				if (ach) {
					ach.info.user = UserCon.get(ach.info.user_id);
					res.push(ach)
				}
			});
			return {
				total_num: total_num,
				total_page: Math.round(total_num / num) || 1, //至少一页
				achs: res,
				page: +page || 0,
				num: +num || 0
			}
		},
		getAchsDetail: function(num, page) {
			var achs = this.getAchs(num, page);
			//包括用户信息
			achs = achs.map(function(ach) {
				ach.info.user = UserCon.get(ach.info.user_id);
				ach.getCash();
				return ach;
			});
			return achs;
		},
		getAchsTotalNum: function() {
			return this.achs.length;
		},
		getAchsDetail_PageInfo: function(num, page) {
			var achs = this.getAchsDetail(num, page);
			var total_num = this.getAchsTotalNum();
			//包括用户信息
			return {
				total_num: total_num,
				total_page: Math.round(total_num / num) || 1, //至少一页
				achs: achs,
				page: +page || 0,
				num: +num || 0
			}
		},
		getAchById: function(ach_id) {
			return this._getAch(ach_id);
		},
		getAchWithEvalById: function(ach_id) {
			var ach = this._getAch(ach_id);
			ach.info.goods_info.forEach(function(goods_info) {
				goods_info.eval = goods_info.eval_id && EvaluationCon.get(goods_info.eval_id).getDetail()
			});
			return ach;
		},
		//代码片段，增添加订单到用户订单列表
		_addAch: function(ach_id) {
			var achs = (this.achs || (this.achs = [])).slice();
			if (achs.every(function(other_ach_id) {
					return other_ach_id !== ach_id
				})) {
				var result = achs.unshift(ach_id);
				this.achs = achs;
				this.save();
				return result;
			} else {
				return false;
			}
		},
		//代码片段，从用户订单列表中移除订单
		_removeAch: function(ach_id) {
			var achs = (this.achs || (this.achs = []));
			var result = $Array.remove(achs, ach_id);
			if (result.length !== achs.length) {
				this.achs = result;
				this.save();
				return result;
			}
			return false;
		},
		//支付订单（第三方支付接口回掉后触发的函数）
		//创建一个订单支付对象，来处理订单的支付功能
		//并支持回掉到自己的线程上
		callBackToPayAch: function(ach_id) {
			var payer = this;
			var ach = AchCon.get(ach_id);
			if (!ach) {
				console.log("订单编号“", ach_id, "”无效");
				throwCusError("订单号无效");
			} else if (ach.info.state === AchCon.STATE.ol未发货) {
				throwCusError("订单已支付")
			} else if (ach.info.state !== AchCon.STATE.ol未付款) {
				throwCusError("错误的订单状态")
			} else if (ach.info.user_id !== this._id) {
				throwCusError("该订单不属于指定用户")
			}
			//生成一个支付信息回调对象，等待回调
			var pa_id = PayAchCon.build(payer._id, ach._id);
			return pa_id;
		},
		//锁定订单，不再修改，解锁需要商家
		//锁定订单后通知商家收到订单
		lockAch: function(ach_id) {
			var ach = this._getAch(ach_id);
			if (!ach) {
				throwCusError("找不到指定订单");
			}
			ach._confirm();
			return ach;
		},
		state_ach_ol未收货_to_ol交易完成: function(ach_id) {
			var ach = this._getAch(ach_id);
			if (ach.info.state == AchCon.STATE.ol未收货) {
				return ach._nextFlowStatus();
			} else {
				throwCusError("订单所在状态无法进行相应转换")
			}
		},
		state_ach_已发货_to_未收货未付款: function(ach_id) {
			var ach = this._getAch(ach_id);
			if (ach.info.state == AchCon.STATE.已发货) {
				return ach._nextFlowStatus();
			} else {
				throwCusError("订单所在状态无法进行相应转换")
			}
		},
		state_ach_ol交易完成_to_ol已评价: function(ach_id, evaluations) {
			var ach = this._getAch(ach_id);
			ach._evalGoods(evaluations);
			if (ach.info.state == AchCon.STATE.ol交易完成) {
				return ach._nextFlowStatus();
			} else {
				throwCusError("订单所在状态无法进行相应转换")
			}
		},
		state_ach_交易完成_to_已评价: function(ach_id, evaluations) {
			var ach = this._getAch(ach_id);
			ach._evalGoods(evaluations);
			if (ach.info.state == AchCon.STATE.交易完成) {
				return ach._nextFlowStatus();
			} else {
				throwCusError("订单所在状态无法进行相应转换")
			}
		},
		state_ach_face交易完成_to_face已评价: function(ach_id, evaluations) {
			var ach = this._getAch(ach_id);
			ach._evalGoods(evaluations);
			if (ach.info.state == AchCon.STATE.face交易完成) {
				return ach._nextFlowStatus();
			} else {
				throwCusError("订单所在状态无法进行相应转换")
			}
		},
		cancelAch: function(ach_id) {
			var ach = this._getAch(ach_id, true);
			ach._cancel();
		},
		//更改订单的支付方式
		updateAchPayMethod: function(ach_id, new_pay_method) {
			var ach = this._getAch(ach_id);
			ach._updateAchPayMethod(new_pay_method);
			//返回完整的订单信息
			return ach;
		},
		//更改收获地址
		updateUserAddress: function(ach_id, user_address_id) {
			var user_address = this.getOneAddress(user_address_id);
			var ach = AchCon.get(ach_id);
			ach._updateUserAddress(user_address);
			return user_address;
		},
		updateSpace: function(ach_id, space_id) {
			var space = BusSpaceCon.get(space_id);
			if (!space) {
				throwCusError("无效的空间编号")
			}
			var ach = AchCon.get(ach_id);
			ach._updateSpace(space);
			//返回完整的订单信息
			return ach;
		},
		updateUseSpace: function(ach_id, use_space) {
			var ach = AchCon.get(ach_id);
			ach._updateUseSpace(use_space);
			return ach;
		}
	};
	module.exports = skill;
	var db = require("../db/index");
	var UserCon = require("./_con");
	var UserCartGoodsCon = require("../user.cart_goods/_con");
	var UserAddressCon = require("../user_address/_con");
	var BusCon = require("../bus/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var BusSpaceCon = require("../bus.space/_con");
	var GoodsCon = require("../goods/_con");
	var AchCon = require("../ach/_con");
	var PayAchCon = require("../pay_ach/_con");
	var CommentCon = require("../comment/_con");
	var EvaluationCon = require("../eval/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var $Array = require("../_include/$.Array");
	var $Object = require("../_include/$.Object");
	var md5 = require("../_include/md5");
	var helper = require("../_include/helper");
}());